package mspbots.timedoctor.client.core.listener;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SyncTimeListener {

    @Autowired
    private ReactiveRedisTemplate<String, Object> reactiveRedisTemplate;

    @RabbitListener(queues = "timedoctor.sync.time")
    public void logSyncTime(SyncTimeDTO dto) {
        try {
            String key = dto.getApi().replaceAll("/api/1.0/", "")
                    .replaceAll("activity/", "")
                    .replaceAll("/", "");
            key = "data-connector:time-doctor:v2:" + key + ":" + dto.getTenantId();
            final String k = key;
            this.reactiveRedisTemplate.opsForValue()
                    .set(key, dto)
                    .subscribe(result -> log.debug("Redis set ({}, {}) success.", k, dto),
                            err -> log.error("Redis set {} error.", k));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Log sync time {} error : {}", dto, e.getMessage());
        }
    }

}
